package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.domain.cargo.ShippingOrderExample;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(value = "/cargo/invoice")
public class InvoiceController extends BaseController {

    @Reference
    private InvoiceService invoiceService;
    @Reference
    private CompanyService companyService;
    @Reference
    private ShippingOrderService shippingOrderService;


    @RequestMapping(value = "/list",name = "发票的分页数据查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize){
        InvoiceExample invoiceExample= new InvoiceExample();
        // 根据发票创建时间排序
        invoiceExample.setOrderByClause("create_time desc");
        PageInfo page = invoiceService.findAll(invoiceExample, pageNum, pageSize);
        request.setAttribute("page",page);
        return "cargo/invoice/invoice-list";
    }

    @RequestMapping(value = "/toAdd",name = "发票的新增页面")
    public String toAdd(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize){
        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        ShippingOrderExample.Criteria criteria = shippingOrderExample.createCriteria();
        criteria.andStateEqualTo(1);
        PageInfo page = shippingOrderService.findAll(shippingOrderExample, pageNum, pageSize);
        request.setAttribute("page",page);
        return "cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/update", name = "编辑委托单")
    public String update(Invoice invoice) {
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }


@RequestMapping(value = "/edit",name = "新增发票单信息")
public String edit(Invoice invoice,String invoiceId){
    System.out.println("invoiceId = " + invoiceId);
    invoice.setInvoiceId(invoiceId);
    invoiceService.save(invoice);
    return "redirect:/cargo/invoice/list.do";
}


    @RequestMapping(value = "/toUpdate",name = "发票编辑页面")
    public String toUpdate(String id){
        // 获取要修改的发票
        Invoice invoice=invoiceService.findById(id);
        request.setAttribute("invoice",invoice);

        return "cargo/invoice/invoice-update";
    }


    @RequestMapping(value = "/delete",name = "发票的删除")
    public String del(String id){
        invoiceService.delete(id);
        // 到最新页面展示最新数据
        return "redirect: /cargo/invoice/list.do";
    }


    @RequestMapping(value = "/toView",name = "发票查看页面")
    public String toView(String id){
        // 获取要修改的发票
        Invoice invoice=invoiceService.findById(id);
        request.setAttribute("invoice",invoice);

        return "cargo/invoice/invoice-view";
    }



    @RequestMapping(value = "/submit",name = "发票的提交")
    public String submit(String id){

        // 根据id获取发票
        Invoice invoice = invoiceService.findById(id);
        // 修改这个发票的state状态
        invoice.setState(1);
        // 修改发票
        invoiceService.update(invoice);
        // 到最新页面展示最新数据
        return "redirect: /cargo/invoice/list.do";
    }

    @RequestMapping(value = "/cancel",name = "发票的取消")
    public String cancel(String id){

        // 根据id获取发票
        Invoice invoice = invoiceService.findById(id);
        // 修改这个发票的state状态
        invoice.setState(0);
        // 修改发票
        invoiceService.update(invoice);
        // 到最新页面展示最新数据
        return "redirect: /cargo/invoice/list.do";
    }


}
